using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace DataStructure
{
    public class HashST2<Key,Value>
    {
        private LinkedList3<Key,Value>[] hashtable;
        private int count;
        private int size;
        
        public HashST2(int size)
        {
            this.size = size;
            count = 0;
            hashtable = new LinkedList3<Key, Value>[size];
            for (int i = 0;i < size; i++)
                hashtable[i] = new LinkedList3<Key, Value>();
        }
        
        public HashST2() : this(97) { }
        
        public int Count => count;
        public bool IsEmpty => count == 0;
        
        private int Hash(Key key)
        {
            return (key.GetHashCode() & 0x7fffffff) % size;
        }

        public void Add(Key key,Value value)
        {
            LinkedList3<Key, Value> list = hashtable[Hash(key)];
            if (list.Contains(key))
                list.Set(key, value);
            else{
                list.Add(key, value);
                count++;
            }
        }
        
        public void Remove(Key key)
        {
            LinkedList3<Key, Value> list = hashtable[Hash(key)];
            if (list.Contains(key))
            {
                list.Remove(key);
                count--;
            }
        }
        
        public bool Contains(Key key)
        {
            return hashtable[Hash(key)].Contains(key);
        }
        
        public Value Get(Key key)
        {
            return hashtable[Hash(key)].Get(key);
        }
        
        public void Set(Key key,Value newValue)
        {
            hashtable[Hash(key)].Set(key, newValue);
        }
    
    }
}